package com.china08.yxyapi.api.manage.tec.teaching.lesson.cloud.ques;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.china08.yxyapi.api.YxyApiBaseController;
import com.china08.yxyapi.entity.db.lessons.cloud.YxyLessonsCloudLessonItemQues;
import com.china08.yxyapi.entity.es.ElasticYxyYxyQuesResource;
import com.china08.yxyapi.model.YxyApiReqModel;
import com.china08.yxyapi.repository.db.lesson.YxyLessonsCloudLessonItemQuesRepository;
import com.china08.yxyapi.service.manage.tec.teaching.lecture.ElasticQuesService;
import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.databind.JsonMappingException;

/**
 * 
 * @annotation: 云课程--编辑课时--查询课时对应试题ID和题干
 * @author: yaoguanglin
 */
@CrossOrigin
@RestController
public class QuesCloudLessonEditSchController extends YxyApiBaseController {

    @Autowired
    private YxyLessonsCloudLessonItemQuesRepository yxyLessonsCloudLessonItemQuesRepository;

    @Autowired
    private ElasticQuesService elasticQuesService;

    @RequestMapping(value = "/api_yw/manage/tec/teaching/lesson/cloud/ques/edit/sch", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public List<RespModel> precess(HttpServletRequest request, HttpServletResponse response,
            @RequestBody(required = false) ReqModel reqModel)
                    throws JsonParseException, JsonMappingException, IOException {

        String itemId = reqModel.itemId;

        List<YxyLessonsCloudLessonItemQues> lessonItemId = yxyLessonsCloudLessonItemQuesRepository
                .findByLessonItemIdOrderByOrderNumAsc(itemId);

        List<String> quesIdList = new ArrayList<String>();
        for (YxyLessonsCloudLessonItemQues yxyLessonsCloudLessonItemQues : lessonItemId) {
            quesIdList.add(yxyLessonsCloudLessonItemQues.getQuesId());
        }
        List<RespModel> list = new ArrayList<RespModel>();
        // 查询题干
        List<ElasticYxyYxyQuesResource> quesIdIn = elasticQuesService.findByQuesIdIn(quesIdList);
        for (ElasticYxyYxyQuesResource elasticYxyYxyQuesResource : quesIdIn) {
            RespModel model = new RespModel();
            model.difficultyLevel = elasticYxyYxyQuesResource.getDifficultyLevel();
            model.id = elasticYxyYxyQuesResource.getId();
            model.knowledgeTexts = elasticYxyYxyQuesResource.getKnowledgeTexts();
            model.knowledgeIds = elasticYxyYxyQuesResource.getKnowledgeIds();
            model.quesBody = elasticYxyYxyQuesResource.getQuesBody();
            model.questionType = elasticYxyYxyQuesResource.getQuestionType();
            model.quesList = quesIdList;
            list.add(model);
        }
        return list;
    }

    protected static class ReqModel extends YxyApiReqModel {
        // 课时ID
        public String itemId;
    }

    protected static class RespModel {
        public String id;
        public String difficultyLevel;
        public List<String> knowledgeTexts;
        public List<Integer> knowledgeIds;
        public String questionType;
        public Object quesBody;
        public boolean favorite;
        public Long quesTimes;
        public List<String> quesList;
    }
}
